package com.fs.controller.user;

import com.fs.dto.ProductAddDTO;
import com.fs.entity.Product;
import com.fs.result.Result;
import com.fs.service.ProductGalleryService;
import com.fs.service.ProductService;
import com.fs.service.ProductTagService;
import com.fs.vo.UserProductDetailVO;
import com.fs.vo.UserProductGalleryVO;
import com.fs.vo.UserProductTagVO;
import com.fs.vo.UserProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品管理控制层
 * 功能：负责接收前端请求并调用Service层处理商品的增删改查逻辑。
 * 模块路径：/admin/product
 */
@RestController("userProductController")
@RequestMapping("/user/product")
@Api(tags = "商品管理接口")
@Slf4j
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductTagService tagService;

    @Autowired
    private ProductGalleryService galleryService;

    /**
     * 用户端查询商品列表（只展示启售商品）
     * 支持按名称、类型、标签搜索
     */
    @GetMapping("/list")
    @ApiOperation("用户端 - 查询商品列表")
    public Result<List<UserProductVO>> list(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "tag", required = false) String tag,
            @RequestParam(value = "sortField", required = false) String sortField,
            @RequestParam(value = "sortOrder", required = false) String sortOrder
    ) {
        log.info("[用户端] 查询商品列表 name={} type={} tag={}", name, type, tag);

        // 查询启售商品
        List<Product> products = (List<Product>) productService.queryByCondition(
                name, type, tag, 1, sortField, sortOrder
        );

        // 转换为用户端VO
        List<UserProductVO> voList = products.stream().map(p -> {
            UserProductVO vo = new UserProductVO();
            BeanUtils.copyProperties(p, vo);
            return vo;
        }).collect(Collectors.toList());

        return Result.success(voList);
    }

    /**
     * 用户端查看商品详情（包含标签 + 图片）
     */
    @GetMapping("/detail/{id}")
    @ApiOperation("用户端 - 查看商品详情")
    public Result<UserProductDetailVO> detail(@PathVariable Long id) {
        log.info("[用户端] 查看商品详情 id={}", id);
        Product product = productService.getById(id);

        if (product == null || product.getStatus() == 0) {
            return Result.error("该商品不存在或已停售");
        }

        // 转换为详情VO
        UserProductDetailVO detailVO = new UserProductDetailVO();
        BeanUtils.copyProperties(product, detailVO);

        // 标签
        List<UserProductTagVO> tags = tagService.listByProductId(id)
                .stream().map(tag -> {
                    UserProductTagVO vo = new UserProductTagVO();
                    vo.setTagName(tag.getTagName());
                    return vo;
                }).collect(Collectors.toList());
        detailVO.setTags(tags);

        // 图片
        List<UserProductGalleryVO> gallery = galleryService.listByProductId(id)
                .stream().map(g -> {
                    UserProductGalleryVO vo = new UserProductGalleryVO();
                    BeanUtils.copyProperties(g, vo);
                    return vo;
                }).collect(Collectors.toList());
        detailVO.setGallery(gallery);

        return Result.success(detailVO);
    }

}
